/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdDatasourceDataIntegrationTask;
import com.comac.ins.isfd.domain.IsfdDatasourceTaskInstanceLog;
import com.comac.ins.isfd.domain.bo.query.IsfdDatasourceTaskExecuteQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdDatasourceTaskInstanceLogQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdDatasourceTaskExecuteVo;
import com.comac.ins.isfd.domain.vo.IsfdDatasourceTaskInstanceLogVo;
import com.comac.ins.isfd.mapper.IsfdDatasourceDataIntegrationTaskMapper;
import com.comac.ins.isfd.mapper.IsfdDatasourceTaskInstanceLogMapper;
import com.comac.ins.isfd.service.IIsfdDatasourceTaskInstanceLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 数据源数据集成任务实例日志Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-11
 */
@RequiredArgsConstructor
@Service
public class IsfdDatasourceTaskInstanceLogServiceImpl implements IIsfdDatasourceTaskInstanceLogService {

    @Autowired
    private IsfdDatasourceTaskInstanceLogMapper baseMapper;

    @Autowired
    private IsfdDatasourceDataIntegrationTaskMapper isfdDatasourceDataIntegrationTaskMapper;

    /**
     * 查询数据源数据集成任务实例日志
     *
     * @param id 主键
     * @return 数据源数据集成任务实例日志
     */
    @Override
    public IsfdDatasourceTaskInstanceLogVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    @Override
    public TableDataInfo<IsfdDatasourceTaskInstanceLogVo> queryPageList(IsfdDatasourceTaskInstanceLogQueryBo queryBo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(queryBo.getPageNum());
        pageQuery.setPageSize(queryBo.getPageSize());
        LambdaQueryWrapper<IsfdDatasourceTaskInstanceLog> lqw = buildQueryWrapper(queryBo);
        Page<IsfdDatasourceTaskInstanceLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<IsfdDatasourceTaskExecuteVo> executeList(IsfdDatasourceTaskExecuteQueryBo bo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(bo.getPageNum());
        pageQuery.setPageSize(bo.getPageSize());
        QueryWrapper<IsfdDatasourceDataIntegrationTask> lqw = Wrappers.query();
        lqw.eq(StringUtils.isNotBlank(bo.getSyncType()), "t.sync_type", bo.getSyncType());
        lqw.eq("t.is_deleted", 0);
        lqw.eq(StringUtils.isNotBlank(bo.getTaskName()), "t.task_name", bo.getTaskName());
        lqw.between(bo.getTriggerEndTime() != null && bo.getTriggerStartTime() != null,
            "t.trigger_time", bo.getTriggerStartTime(), bo.getTriggerEndTime());
        lqw.orderByDesc("t.trigger_time");
        return TableDataInfo.build(isfdDatasourceDataIntegrationTaskMapper.executeList(pageQuery.build(), lqw));
    }

    private LambdaQueryWrapper<IsfdDatasourceTaskInstanceLog> buildQueryWrapper(IsfdDatasourceTaskInstanceLogQueryBo bo) {
        LambdaQueryWrapper<IsfdDatasourceTaskInstanceLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, IsfdDatasourceTaskInstanceLog::getTaskId, bo.getTaskId());
        lqw.eq(IsfdDatasourceTaskInstanceLog::getIsDeleted, 0);
        lqw.orderByDesc(IsfdDatasourceTaskInstanceLog::getCreateBy);
        return lqw;
    }

}
